package com.pioneer.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.system.domain.SysProject;
import com.pioneer.web.system.domain.SysUser;
import com.pioneer.web.system.service.ISysProjectService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 项目管理
 *
 * @author hlm
 * @date 2021-08-09 17:52:04
 */
@RestController
@RequestMapping("/system/project")
public class SysProjectController extends BaseController {

    @Resource
    private ISysProjectService projectService;

    /**
     * 获取公司项目列表
     *
     * @param sysProject 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:project:list')")
    @GetMapping("/list")
    public AjaxResult list(SysProject sysProject) {
        boolean isPage = startPage();
        LambdaQueryWrapper<SysProject> queryWrapper = Wrappers.lambdaQuery(sysProject).orderByDesc(SysProject::getProjectId);
        List<SysProject> list = projectService.list(queryWrapper);
        return isPage ? getDataTable(list) : AjaxResult.success(list);
    }

    /**
     * 根据公司项目id获取详细信息
     *
     * @param projectId 公司项目id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:project:query')")
    @GetMapping(value = "/{projectId}")
    public AjaxResult getInfo(@PathVariable Long projectId) {
        return AjaxResult.success(projectService.getById(projectId));
    }

    /**
     * 新增项目
     *
     * @param sysProject 公司项目
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:project:add')")
    @Log(title = "公司项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysProject sysProject) {
        SysUser user = getLoginUser().getUser();
        sysProject.setCreateBy(user.getNickName());
        return toAjax(projectService.save(sysProject));
    }

    /**
     * 修改公司项目
     *
     * @param sysProject 公司项目
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('system:project:edit')")
    @Log(title = "公司项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysProject sysProject) {
        sysProject.setUpdateBy(getUsername());
        return toAjax(projectService.updateById(sysProject));
    }

    /**
     * 删除公司项目
     *
     * @param projectIds 公司项目id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('school:active:remove')")
    @Log(title = "公司项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(projectService.removeByIds(Arrays.asList(projectIds)));
    }
}
